﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class SubsetInArrayByGivenNumbers
{
    static void Main(string[] args)
    {
        Console.Write("Insert how long will be your array : ");
        int n = int.Parse(Console.ReadLine());
        int[] MyArray = new int[n];
        for (int i = 0; i < MyArray.Length; i++)
        {
            Console.Write("{0} : " , i+1);
            MyArray[i] = int.Parse(Console.ReadLine());
        }
        Console.Write("Insert sum : ");
        int sum = int.Parse(Console.ReadLine());
        Console.Write("Insert K : ");
        int k = int.Parse(Console.ReadLine());
        int MaxCombination = (int)Math.Pow(2, MyArray.Length) - 1;
        for (int i = 1; i <= MaxCombination; i++)
        {
            int count = 0;
            string CurrentSubset = "";
            int tempSum = 0;
            for (int j = 0; j < MyArray.Length; j++)
            {
                
                int mask = 1 << j;
                int nAndMask = i & mask;
                int bit = nAndMask >> j;
                if (bit == 1)
                {
                    if (count > k)
                    {
                        break;
                    }
                    tempSum += MyArray[j];
                    CurrentSubset += MyArray[j].ToString() + " ";
                    count++;
                }

            }
            if (tempSum == sum && count==k)
            {
                Console.WriteLine("Subset {0}have sum {1}", CurrentSubset, sum);
            }
        }
    }
}
